﻿using System.Runtime.CompilerServices;
using System.Windows.Input;
using KinonekoSoftware.Foundation;

namespace KinonekoSoftware.Foundation.UI.ViewModels
{
    public abstract class DialogBase : ViewModelBase
    {
        private string        _cancelButtonText;
        private string        _description;
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="parameter"></param>
        protected sealed override void OnReceive(object parameter)
        {
            if (parameter is not DialogContext dc)
            {
                return;
            }
            OnReceiveInternal(dc);
        }
        
        internal virtual void OnReceiveInternal(DialogContext dc){}
        
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        protected abstract bool CanFinish();
        
        /// <summary>
        /// 设置属性值，并提示数据更新
        /// </summary>
        /// <param name="source">原始的字段</param>
        /// <param name="value">值字段</param>
        /// <param name="name">属性名</param>
        /// <typeparam name="E">类型数据</typeparam>
        /// <returns>返回是否更新</returns>
        protected bool TryFinishAndSetValue<E>(ref E source, E value, [CallerMemberName] string name = "")
        {
            if (string.IsNullOrEmpty(name)) return false;
            source = value;
            RaiseUpdated(name);
            TryFinish();
            
            return true;
        }

        protected virtual void TryFinish()
        {
        }

        /// <summary>
        /// 获取或设置 <see cref="CancelButtonText"/> 属性。
        /// </summary>
        public string CancelButtonText
        {
            get => _cancelButtonText;
            set => SetValue(ref _cancelButtonText, value);
        }
        
        /// <summary>
        /// 获取或设置 <see cref="Description"/> 属性。
        /// </summary>
        public string Description
        {
            get => _description;
            set => SetValue(ref _description, value);
        }
        
        /// <summary>
        /// 
        /// </summary>
        public ICommand CancelCommand { get; protected set; }
    }
}